package ocn_psn.ocnpsn;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ScrollView;

import java.util.Random;

/**
 * Created by Olivier on 29/01/2015.
 */
public class GameActivity extends Activity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener, View.OnClickListener {

    private Button tabBtnPlayer[] = new Button[64];
    private Button tabBtnIA[] = new Button[64];
    private int idTabPlayer[][]=new int[8][8];
    private int idTabIA[][]=new int[8][8];
    //Tableaux prennant 3 valeurs : 0=eau, 1=bateau, 2=tiré
    private int statusTabPlayer[][] = new int[8][8];
    private int statusTabIA[][] = new int[8][8];
    private int nbOfHpLeft[] = new int[2];

    private GestureDetectorCompat mDetector;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        mDetector = new GestureDetectorCompat(this,this);
        mDetector.setOnDoubleTapListener(this);


        FrameLayout gameView = (FrameLayout) findViewById(R.id.gameView);
        Intent intent = getIntent();
        boolean nightMode = intent.getBooleanExtra("nightMode", false);
        boolean autoMode = intent.getBooleanExtra("autoMode", false);
        if (nightMode)
        {
            gameView.setBackgroundColor(Color.BLACK);
        }
        else
        {
            gameView.setBackgroundColor(Color.WHITE);
        }

        //Préparation du tableau
        resetStatusTab(statusTabPlayer);
        resetStatusTab(statusTabIA);
        fonctionDégueulasse();
        remplirIdTab();
        for (int i = 0; i< tabBtnPlayer.length; i++)
        {
            tabBtnPlayer[i].setOnClickListener(this);
        }

        //Préparation du switch de vue
        ScrollView scroll2 = (ScrollView) findViewById(R.id.scrollView2);
        scroll2.setVisibility(View.INVISIBLE);
        Button btnFin = (Button) findViewById(R.id.btnFin);
        btnFin.setVisibility(View.INVISIBLE);
        Button btnValider = (Button) findViewById(R.id.btnValider);
        btnValider.setVisibility(View.INVISIBLE);
        Button btnAgain = (Button) findViewById(R.id.btnAgain);
        btnAgain.setVisibility(View.INVISIBLE);


        //checkForCreation(false,3,0,0, true);
        //checkForCreation(true,5, 0, 5, true);

        //Création des bateaux du joueur
        if(autoMode)
        {
            btnValider.setVisibility(View.VISIBLE);
            btnAgain.setVisibility(View.VISIBLE);
            switchView();
            createRandomShips(true);
            btnValider.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Button btnValider = (Button) findViewById(R.id.btnValider);
                    btnValider.setVisibility(View.INVISIBLE);
                    Button btnAgain = (Button) findViewById(R.id.btnAgain);
                    btnAgain.setVisibility(View.INVISIBLE);
                }
            });
            btnAgain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetStatusTab(statusTabIA);
                    for(int i=0; i<tabBtnIA.length; i++)
                    {
                        tabBtnIA[i].setBackgroundColor(Color.LTGRAY);
                    }
                    createRandomShips(true);
                }
            });

        }
        else
        {
            createRandomShips(true);
        }

        //Création des bateaux de l'IA
        createRandomShips(false);
    }

    public void onClick(View view) {
        Button btnValider = (Button) findViewById(R.id.btnValider);
        btnValider.setVisibility(View.INVISIBLE);
        Button btnAgain = (Button) findViewById(R.id.btnAgain);
        btnAgain.setVisibility(View.INVISIBLE);
        view.setEnabled(false);
        attackAPosition(true,((Button) view));

        //L'IA attaque à son tour
        boolean hasPlayed = false;
        while (!hasPlayed)
        {
            Random rand = new Random();
            int randI =  rand.nextInt(8);
            int randJ =  rand.nextInt(8);
            if (statusTabIA[randI][randJ]!=2)
            {
                attackAPosition(false, getButtonFromPosOfIA(randI, randJ));
                hasPlayed=true;
            }
        }

    }

    /*
        FONCTIONS DE GESTION DU JEU
     */

    public void createRandomShips(boolean player)
    {
        for (int size= 2; size <6; size++)
        {
            Random rand = new Random();
            int randI =  rand.nextInt(8);
            int randJ =  rand.nextInt(8);
            boolean randDir = rand.nextBoolean();
            if(!checkForCreation(randDir, size, randI, randJ, player))
            {
                size--;
            }
        }
    }

    public boolean checkForCreation(boolean dir, int taille, int h, int l, boolean player)
    {
        if (player)
        {
            if(dir){
                for (int i=h;i<h+taille;i++)
                {
                    if(i>7 || statusTabIA[i][l]!=0)
                    {
                        return false;
                    }
                }
                Ship ship = new Ship(dir, taille, h, l);
                creationBateau(ship, player);
                return true;
            }
            else
            {
                for (int i=l;i<l+taille;i++)
                {
                    if(i>7 || statusTabIA[h][i]!=0)
                    {
                        return false;
                    }
                }
                Ship ship = new Ship(dir, taille, h, l);
                creationBateau(ship, player);
                return true;
            }
        }
        else
        {
            if(dir){
                for (int i=h;i<h+taille;i++)
                {
                    if(i>7 || statusTabPlayer[i][l]!=0)
                    {
                        return false;
                    }
                }
                Ship ship = new Ship(dir, taille, h, l);
                creationBateau(ship, player);
                return true;
            }
            else
            {
                for (int i=l;i<l+taille;i++)
                {
                    if(i>7 || statusTabPlayer[h][i]!=0)
                    {
                        return false;
                    }
                }
                Ship ship = new Ship(dir, taille, h, l);
                creationBateau(ship, player);
                return true;
            }
        }

    }

    public void creationBateau(Ship ship, boolean player){
        //Si on crée le bateau pour un joueur, donc on l'affiche sur le tableau du bas

        Intent intent = getIntent();
        boolean nightMode =intent.getBooleanExtra("nightMode", false);
        boolean cheatMode =intent.getBooleanExtra("cheatMode", false);
        int colorOfShip;
        if (nightMode)
        {
            colorOfShip = Color.YELLOW;
        }
        else
        {
            colorOfShip = Color.GREEN;
        }

        if (player)
        {
            if(ship.isDirection()){
                for (int i=ship.getHauteurDebut();i<ship.getHauteurDebut()+ship.getLongeur();i++)
                {
                    getButtonFromPosOfIA(i, ship.getLargeurDebut()).setBackgroundColor(colorOfShip);
                    statusTabIA[i][ship.getLargeurDebut()]=1;
                    nbOfHpLeft[0]++;
                }
            }
            else
            {
                for (int i=ship.getLargeurDebut();i<ship.getLargeurDebut()+ship.getLongeur();i++)
                {
                    getButtonFromPosOfIA(ship.getHauteurDebut(), i).setBackgroundColor(colorOfShip);
                    statusTabIA[ship.getHauteurDebut()][i]=1;
                    nbOfHpLeft[0]++;
                }
            }
        }
        //Si on crée le bateau pour l'ordinateur
        else
        {
            if(ship.isDirection()){
                for (int i=ship.getHauteurDebut();i<ship.getHauteurDebut()+ship.getLongeur();i++)
                {

                    if (cheatMode)
                    {
                        getButtonFromPosOfPlayer(i, ship.getLargeurDebut()).setBackgroundColor(colorOfShip);
                    }
                    statusTabPlayer[i][ship.getLargeurDebut()]=1;
                    nbOfHpLeft[1]++;
                }
            }
            else
            {
                for (int i=ship.getLargeurDebut();i<ship.getLargeurDebut()+ship.getLongeur();i++)
                {
                    if (cheatMode)
                    {
                        getButtonFromPosOfPlayer(ship.getHauteurDebut(), i).setBackgroundColor(colorOfShip);
                    }
                    statusTabPlayer[ship.getHauteurDebut()][i]=1;
                    nbOfHpLeft[1]++;
                }
            }
        }
    }

    public void attackAPosition(boolean player, Button button)
    {
        Intent intent = getIntent();
        boolean nightMode =intent.getBooleanExtra("nightMode", false);
        int colorOfHit, colorOfWater;
        if (nightMode)
        {
            colorOfHit = Color.RED;
            colorOfWater = Color.TRANSPARENT;
        }
        else
        {
            colorOfHit = Color.RED;
            colorOfWater = Color.BLUE;
        }

        //Le joueur attaque
        if (player)
        {
            int hauteur = getHFromButtonOfPlayer(button);
            int largeur = getLFromButtonOfPlayer(button);
            switch (statusTabPlayer[hauteur][largeur])
            {
                //Il tire sur de l'eau
                case 0:
                    getButtonFromPosOfPlayer(hauteur, largeur).setBackgroundColor(colorOfWater);
                    statusTabPlayer[hauteur][largeur]=2;
                    break;
                //Il tire sur un bateau
                case 1:
                    getButtonFromPosOfPlayer(hauteur, largeur).setBackgroundColor(colorOfHit);
                    statusTabPlayer[hauteur][largeur]=2;
                    nbOfHpLeft[1]--;
                    endOfGame(false);
                    break;
                //Il a déjà visé cette case (couvert par le bouton désactivé)
                case 2:
                    break;
            }
        }
        //L'ordinateur attaque
        else
        {
            int hauteur = getHFromButtonOfIA(button);
            int largeur = getLFromButtonOfIA(button);
            switch (statusTabIA[hauteur][largeur])
            {
                //Il tire sur de l'eau
                case 0:
                    getButtonFromPosOfIA(hauteur, largeur).setBackgroundColor(colorOfWater);
                    statusTabIA[hauteur][largeur]=2;
                    break;
                //Il tire sur un bateau
                case 1:
                    getButtonFromPosOfIA(hauteur, largeur).setBackgroundColor(colorOfHit);
                    statusTabIA[hauteur][largeur]=2;
                    nbOfHpLeft[0]--;
                    endOfGame(true);
                    break;
                //Il a déjà visé cette case
                case 2:
                    break;
            }
        }
    }

    public void endOfGame(boolean player)
    {
        Button btnFin = (Button) findViewById(R.id.btnFin);
        btnFin.setTextSize(8);
        btnFin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //On vérifie que le joueur a encore des bateaux
        if (player)
        {
            if (nbOfHpLeft[0]==0)
            {
                btnFin.setVisibility(View.VISIBLE);
                desactivateAllButtons();
                btnFin.setText("Défaite !");
            }
        }
        else
        {
            if (nbOfHpLeft[1]==0)
            {
                btnFin.setVisibility(View.VISIBLE);
                desactivateAllButtons();
                btnFin.setText("Victoire !");
            }
        }
    }

    public void desactivateAllButtons()
    {
        for (int i = 0; i< tabBtnPlayer.length; i++)
        {
            tabBtnPlayer[i].setEnabled(false);
        }
    }

     /*
        FONCTIONS D'ACCESSIONS AUX INFORMATIONS DES BOUTONS

     */
    public int getHFromButtonOfPlayer(Button button)
    {
        for (int i=0;i<8;i++)
        {
            for (int j=0; j<8; j++)
            {
                if (getButtonFromPosOfPlayer(i, j)==button)
                {
                    return i;
                }
            }
        }
        return -1;
    }
    public int getLFromButtonOfPlayer(Button button)
    {
        for (int i=0;i<8;i++)
        {
            for (int j=0; j<8; j++)
            {
                if (getButtonFromPosOfPlayer(i, j)==button)
                {
                    return j;
                }
            }
        }
        return -1;
    }
    public int getHFromButtonOfIA(Button button)
    {
        for (int i=0;i<8;i++)
        {
            for (int j=0; j<8; j++)
            {
                if (getButtonFromPosOfIA(i, j)==button)
                {
                    return i;
                }
            }
        }
        return -1;
    }
    public int getLFromButtonOfIA(Button button)
    {
        for (int i=0;i<8;i++)
        {
            for (int j=0; j<8; j++)
            {
                if (getButtonFromPosOfIA(i, j)==button)
                {
                    return j;
                }
            }
        }
        return -1;
    }
    public Button getButtonFromIdOfPlayer(int id)
    {
        return tabBtnPlayer[id];
    }
    public Button getButtonFromIdOfIA(int id)
    {
        return tabBtnIA[id-100];
    }
    public Button getButtonFromPosOfPlayer(int h, int l)
    {
        return  getButtonFromIdOfPlayer(getIdFromPosOfPlayer(h, l));
    }
    public Button getButtonFromPosOfIA(int h, int l)
    {
        return  getButtonFromIdOfIA(getIdFromPosOfIA(h, l));
    }
    public int getIdFromPosOfPlayer(int h, int l){
        return idTabPlayer[h][l];
    }
    public int getIdFromPosOfIA(int h, int l){
        return idTabIA[h][l];
    }
    public int getIdFromButton(Button btn)
    {
        for ( int btnH = 0; btnH<8; btnH++)
        {
            for ( int btnL = 0; btnL<8; btnL++)
            {
                if (tabBtnPlayer[getIdFromPosOfPlayer(btnH, btnL)]==btn)
                {
                    return getIdFromPosOfPlayer(btnH, btnL);
                }
            }
        }
        return -1;
    }

    /*
        FONCTIONS DE GESTION DE LA VUE
    */
    public void switchView()
    {
        final ScrollView scroll1 = (ScrollView) findViewById(R.id.scrollView);
        final ScrollView scroll2 = (ScrollView) findViewById(R.id.scrollView2);

        if (scroll1.getVisibility()==View.VISIBLE){
            scroll1.setVisibility(View.INVISIBLE);
            scroll2.setVisibility((View.VISIBLE));
        }
        else
        {
            scroll1.setVisibility(View.VISIBLE);
            scroll2.setVisibility((View.INVISIBLE));
        }
    }

    /*
        FONCTIONS D'INITIALISATION
    */
    public void remplirIdTab()
    {
        int inc=0;
        for (int i=0;i<8;i++){
            for (int j=0; j<8;j++){
                idTabPlayer[i][j]=inc;
                idTabIA[i][j]=inc+100;
                inc++;
            }
        }
    }
    public void resetStatusTab(int tab[][])
    {
        for (int i=0; i<8;i++)
        {
            for (int j=0; j<8;j++)
            {
                tab[i][j] = 0;
            }
        }
    }

    public void belleFonction()
    {
        for (int i = 0; i< tabBtnPlayer.length; i++){
            String nomI = "R.id.btn"+Integer.toString(i);
            int entierI = Integer.parseInt(nomI);
            tabBtnPlayer[i] = (Button) findViewById(entierI);
        }

    }

    public void fonctionDégueulasse()
    {
        tabBtnPlayer[0] = (Button) findViewById(R.id.btn0);
        tabBtnPlayer[1] = (Button) findViewById(R.id.btn1);
        tabBtnPlayer[2] = (Button) findViewById(R.id.btn2);
        tabBtnPlayer[3] = (Button) findViewById(R.id.btn3);
        tabBtnPlayer[4] = (Button) findViewById(R.id.btn4);
        tabBtnPlayer[5] = (Button) findViewById(R.id.btn5);
        tabBtnPlayer[6] = (Button) findViewById(R.id.btn6);
        tabBtnPlayer[7] = (Button) findViewById(R.id.btn7);
        tabBtnPlayer[8] = (Button) findViewById(R.id.btn8);
        tabBtnPlayer[9] = (Button) findViewById(R.id.btn9);
        tabBtnPlayer[10] = (Button) findViewById(R.id.btn10);
        tabBtnPlayer[11] = (Button) findViewById(R.id.btn11);
        tabBtnPlayer[12] = (Button) findViewById(R.id.btn12);
        tabBtnPlayer[13] = (Button) findViewById(R.id.btn13);
        tabBtnPlayer[14] = (Button) findViewById(R.id.btn14);
        tabBtnPlayer[15] = (Button) findViewById(R.id.btn15);
        tabBtnPlayer[16] = (Button) findViewById(R.id.btn16);
        tabBtnPlayer[17] = (Button) findViewById(R.id.btn17);
        tabBtnPlayer[18] = (Button) findViewById(R.id.btn18);
        tabBtnPlayer[19] = (Button) findViewById(R.id.btn19);
        tabBtnPlayer[20] = (Button) findViewById(R.id.btn20);
        tabBtnPlayer[21] = (Button) findViewById(R.id.btn21);
        tabBtnPlayer[22] = (Button) findViewById(R.id.btn22);
        tabBtnPlayer[23] = (Button) findViewById(R.id.btn23);
        tabBtnPlayer[24] = (Button) findViewById(R.id.btn24);
        tabBtnPlayer[25] = (Button) findViewById(R.id.btn25);
        tabBtnPlayer[26] = (Button) findViewById(R.id.btn26);
        tabBtnPlayer[27] = (Button) findViewById(R.id.btn27);
        tabBtnPlayer[28] = (Button) findViewById(R.id.btn28);
        tabBtnPlayer[29] = (Button) findViewById(R.id.btn29);
        tabBtnPlayer[30] = (Button) findViewById(R.id.btn30);
        tabBtnPlayer[31] = (Button) findViewById(R.id.btn31);
        tabBtnPlayer[32] = (Button) findViewById(R.id.btn32);
        tabBtnPlayer[33] = (Button) findViewById(R.id.btn33);
        tabBtnPlayer[34] = (Button) findViewById(R.id.btn34);
        tabBtnPlayer[35] = (Button) findViewById(R.id.btn35);
        tabBtnPlayer[36] = (Button) findViewById(R.id.btn36);
        tabBtnPlayer[37] = (Button) findViewById(R.id.btn37);
        tabBtnPlayer[38] = (Button) findViewById(R.id.btn38);
        tabBtnPlayer[39] = (Button) findViewById(R.id.btn39);
        tabBtnPlayer[40] = (Button) findViewById(R.id.btn40);
        tabBtnPlayer[41] = (Button) findViewById(R.id.btn41);
        tabBtnPlayer[42] = (Button) findViewById(R.id.btn42);
        tabBtnPlayer[43] = (Button) findViewById(R.id.btn43);
        tabBtnPlayer[44] = (Button) findViewById(R.id.btn44);
        tabBtnPlayer[45] = (Button) findViewById(R.id.btn45);
        tabBtnPlayer[46] = (Button) findViewById(R.id.btn46);
        tabBtnPlayer[47] = (Button) findViewById(R.id.btn47);
        tabBtnPlayer[48] = (Button) findViewById(R.id.btn48);
        tabBtnPlayer[49] = (Button) findViewById(R.id.btn49);
        tabBtnPlayer[50] = (Button) findViewById(R.id.btn50);
        tabBtnPlayer[51] = (Button) findViewById(R.id.btn51);
        tabBtnPlayer[52] = (Button) findViewById(R.id.btn52);
        tabBtnPlayer[53] = (Button) findViewById(R.id.btn53);
        tabBtnPlayer[54] = (Button) findViewById(R.id.btn54);
        tabBtnPlayer[55] = (Button) findViewById(R.id.btn55);
        tabBtnPlayer[56] = (Button) findViewById(R.id.btn56);
        tabBtnPlayer[57] = (Button) findViewById(R.id.btn57);
        tabBtnPlayer[58] = (Button) findViewById(R.id.btn58);
        tabBtnPlayer[59] = (Button) findViewById(R.id.btn59);
        tabBtnPlayer[60] = (Button) findViewById(R.id.btn60);
        tabBtnPlayer[61] = (Button) findViewById(R.id.btn61);
        tabBtnPlayer[62] = (Button) findViewById(R.id.btn62);
        tabBtnPlayer[63] = (Button) findViewById(R.id.btn63);

        tabBtnIA[0] = (Button) findViewById(R.id.btn100);
        tabBtnIA[1] = (Button) findViewById(R.id.btn101);
        tabBtnIA[2] = (Button) findViewById(R.id.btn102);
        tabBtnIA[3] = (Button) findViewById(R.id.btn103);
        tabBtnIA[4] = (Button) findViewById(R.id.btn104);
        tabBtnIA[5] = (Button) findViewById(R.id.btn105);
        tabBtnIA[6] = (Button) findViewById(R.id.btn106);
        tabBtnIA[7] = (Button) findViewById(R.id.btn107);
        tabBtnIA[8] = (Button) findViewById(R.id.btn108);
        tabBtnIA[9] = (Button) findViewById(R.id.btn109);
        tabBtnIA[10] = (Button) findViewById(R.id.btn110);
        tabBtnIA[11] = (Button) findViewById(R.id.btn111);
        tabBtnIA[12] = (Button) findViewById(R.id.btn112);
        tabBtnIA[13] = (Button) findViewById(R.id.btn113);
        tabBtnIA[14] = (Button) findViewById(R.id.btn114);
        tabBtnIA[15] = (Button) findViewById(R.id.btn115);
        tabBtnIA[16] = (Button) findViewById(R.id.btn116);
        tabBtnIA[17] = (Button) findViewById(R.id.btn117);
        tabBtnIA[18] = (Button) findViewById(R.id.btn118);
        tabBtnIA[19] = (Button) findViewById(R.id.btn119);
        tabBtnIA[20] = (Button) findViewById(R.id.btn120);
        tabBtnIA[21] = (Button) findViewById(R.id.btn121);
        tabBtnIA[22] = (Button) findViewById(R.id.btn122);
        tabBtnIA[23] = (Button) findViewById(R.id.btn123);
        tabBtnIA[24] = (Button) findViewById(R.id.btn124);
        tabBtnIA[25] = (Button) findViewById(R.id.btn125);
        tabBtnIA[26] = (Button) findViewById(R.id.btn126);
        tabBtnIA[27] = (Button) findViewById(R.id.btn127);
        tabBtnIA[28] = (Button) findViewById(R.id.btn128);
        tabBtnIA[29] = (Button) findViewById(R.id.btn129);
        tabBtnIA[30] = (Button) findViewById(R.id.btn130);
        tabBtnIA[31] = (Button) findViewById(R.id.btn131);
        tabBtnIA[32] = (Button) findViewById(R.id.btn132);
        tabBtnIA[33] = (Button) findViewById(R.id.btn133);
        tabBtnIA[34] = (Button) findViewById(R.id.btn134);
        tabBtnIA[35] = (Button) findViewById(R.id.btn135);
        tabBtnIA[36] = (Button) findViewById(R.id.btn136);
        tabBtnIA[37] = (Button) findViewById(R.id.btn137);
        tabBtnIA[38] = (Button) findViewById(R.id.btn138);
        tabBtnIA[39] = (Button) findViewById(R.id.btn139);
        tabBtnIA[40] = (Button) findViewById(R.id.btn140);
        tabBtnIA[41] = (Button) findViewById(R.id.btn141);
        tabBtnIA[42] = (Button) findViewById(R.id.btn142);
        tabBtnIA[43] = (Button) findViewById(R.id.btn143);
        tabBtnIA[44] = (Button) findViewById(R.id.btn144);
        tabBtnIA[45] = (Button) findViewById(R.id.btn145);
        tabBtnIA[46] = (Button) findViewById(R.id.btn146);
        tabBtnIA[47] = (Button) findViewById(R.id.btn147);
        tabBtnIA[48] = (Button) findViewById(R.id.btn148);
        tabBtnIA[49] = (Button) findViewById(R.id.btn149);
        tabBtnIA[50] = (Button) findViewById(R.id.btn150);
        tabBtnIA[51] = (Button) findViewById(R.id.btn151);
        tabBtnIA[52] = (Button) findViewById(R.id.btn152);
        tabBtnIA[53] = (Button) findViewById(R.id.btn153);
        tabBtnIA[54] = (Button) findViewById(R.id.btn154);
        tabBtnIA[55] = (Button) findViewById(R.id.btn155);
        tabBtnIA[56] = (Button) findViewById(R.id.btn156);
        tabBtnIA[57] = (Button) findViewById(R.id.btn157);
        tabBtnIA[58] = (Button) findViewById(R.id.btn158);
        tabBtnIA[59] = (Button) findViewById(R.id.btn159);
        tabBtnIA[60] = (Button) findViewById(R.id.btn160);
        tabBtnIA[61] = (Button) findViewById(R.id.btn161);
        tabBtnIA[62] = (Button) findViewById(R.id.btn162);
        tabBtnIA[63] = (Button) findViewById(R.id.btn163);
    }











    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        switchView();
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        int SWIPE_THRESHOLD = 100;
        int SWIPE_VELOCITY_THRESHOLD = 100;
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            }
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffY > 0) {
                    onSwipeBottom();
                } else {
                    onSwipeTop();
                }
            }
            result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
        //Sur un mouvement vers le haut, affiche le tableau du bas
        final ScrollView scroll1 = (ScrollView) findViewById(R.id.scrollView);
        final ScrollView scroll2 = (ScrollView) findViewById(R.id.scrollView2);
        scroll1.setVisibility(View.INVISIBLE);
        scroll2.setVisibility((View.VISIBLE));
    }

    public void onSwipeBottom() {
        //Sur un mouvement vers le bas, affiche le tableau du haut
        final ScrollView scroll1 = (ScrollView) findViewById(R.id.scrollView);
        final ScrollView scroll2 = (ScrollView) findViewById(R.id.scrollView2);
        scroll1.setVisibility(View.VISIBLE);
        scroll2.setVisibility((View.INVISIBLE));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        this.mDetector.onTouchEvent(event);
        // Be sure to call the superclass implementation
        return super.onTouchEvent(event);
    }
}